//package com.xiaoma.tpo.webservice;
//
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestHeader;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import com.xiaoma.tpo.domain.MkListening;
//import com.xiaoma.tpo.domain.MkListeningQuestion;
//import com.xiaoma.tpo.domain.MkReadingQuestion;
//import com.xiaoma.tpo.domain.MkResult;
//import com.xiaoma.tpo.domain.MkSpeakingQuestion;
//import com.xiaoma.tpo.domain.MkWritingQuestion;
//import com.xiaoma.tpo.domain.User;
//import com.xiaoma.tpo.dto.MkListeningQuestionVo;
//import com.xiaoma.tpo.dto.MkReadingQuestionVo;
//import com.xiaoma.tpo.dto.MkReadingVo;
//import com.xiaoma.tpo.dto.MkSingleAnswerVo;
//import com.xiaoma.tpo.dto.MockResultDto;
//import com.xiaoma.tpo.resource.TPOConstants;
//import com.xiaoma.tpo.service.MkSingleAnswerService;
//import com.xiaoma.tpo.service.MockService;
//import com.xiaoma.tpo.service.UserService;
//import com.xiaoma.tpo.utils.Pager;
//import com.xiaoma.tpo.utils.TPOUtils;
//
//@Controller
//@RequestMapping("/service/mock")
//public class MockWS {
//
//    private static Logger LOG = LoggerFactory.getLogger(MockWS.class);
//    
//    @Autowired
//    private MockService mockService;
//    
//    @Autowired
//    private UserService userService;
//    
//    @Autowired
//    private MkSingleAnswerService MkSingleAnswerService;
//    
//    
//    @RequestMapping(value = "/tpos", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findAllTpos() {
//        Map<String, Object> result = new HashMap<String, Object>();
//        result.put("tpos", mockService.findAllTpos());
//        return result;
//    }
//    
//    @RequestMapping(value = "/tpos/{tpoId}/readings", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findReadingsByTpoId(@PathVariable Integer tpoId) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        List<MkReadingVo> tpoReadings = mockService.findReadingsByTpoId(tpoId);
//        result.put("readings", tpoReadings);
//        return result;
//    }
//    
//    
//    @RequestMapping(value = "/tpos/{tpoId}/reading/questions", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findReadingQuestionsByTpoId(@PathVariable Integer tpoId) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        List<MkReadingQuestion> readingQuestions = mockService.findReadingQuestionsByTpoId(tpoId);
//        result.put("readingQuestions", readingQuestions);
//        return result;
//    }
//    
//    @RequestMapping(value = "/reading/questions/{id}", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findReadingQuestionById(@PathVariable Integer id) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        MkReadingQuestion readingQuestion = mockService.findReadingQuestionById(id);
//        result.put("readingQuestion", readingQuestion);
//        return result;
//    }
//    
//    @RequestMapping(value = "/tpos/{tpoId}/listenings", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findListeningsByTpoId(@PathVariable Integer tpoId) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        List<MkListening> listenings = mockService.findListeningsByTpoId(tpoId);
//        result.put("listenings", listenings);
//        return result;
//    }
//    
//    @RequestMapping(value = "/tpos/{tpoId}/listening/questions", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findListeningQuestionsByTpoId(@PathVariable Integer tpoId) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        List<MkListeningQuestion> listeningQuestions = mockService.findListeningQuestionsByTpoId(tpoId);
//        result.put("listeningQuestions", listeningQuestions);
//        return result;
//    }
//    
//    @RequestMapping(value = "/listening/questions/{id}", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findListeningQuestionById(@PathVariable Integer id) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        MkListeningQuestion listeningQuestion = mockService.findListeningQuestionById(id);
//        result.put("listeningQuestion", listeningQuestion);
//        return result;
//    }
//    
//    @RequestMapping(value = "/tpos/{tpoId}/speaking/questions", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findSpeakingQuestionsByTpoId(@PathVariable Integer tpoId) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        List<MkSpeakingQuestion> speakingQuestions = mockService.findSpeakingQuestionsByTpoId(tpoId);
//        result.put("speakingQuestions", speakingQuestions);
//        return result;
//    }
//    
//    @RequestMapping(value = "/tpos/{tpoId}/writing/questions", method = RequestMethod.GET)
//    @ResponseBody
//    public Map<String, Object> findWritingingQuestionsByTpoId(@PathVariable Integer tpoId) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        List<MkWritingQuestion> writingQuestions = mockService.findWritingQuestionsByTpoId(tpoId);
//        result.put("writingQuestions", writingQuestions);
//        return result;
//    }
//    
//    @RequestMapping(value = "/reading/result", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> saveReadingResult(@RequestHeader String token, @RequestBody MockResultDto resultDto) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        User user = userService.findByToken(token);
//        if(null == user) {
//            result.put("status", -2);
//            result.put("message", "invalid token");
//            return result;
//        }
//        if (TPOUtils.isAnonymous(user)) {
//            result.put("status", 0);
//            result.put("message", "anonymous can't access");
//            return result;
//            
//        }
//        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
//            result.put("status", -1);
//            result.put("message", "token disabled over 7 days, please login");
//            return result;
//        }
//        
//        MkResult mockResult = new MkResult();
//        Date now = new Date();
//        mockResult.setUserId(user.getId());
//        mockResult.setScore(resultDto.getScore());
//        mockResult.setRightCount(resultDto.getRightCount());
//        mockResult.setTotalCount(resultDto.getTotalCount());
//        mockResult.setMkTpoId(resultDto.getTpoId());
//        mockResult.setType(TPOConstants.READING);
//        mockResult.setCreatedAt(now);
//        mockResult.setUpdatedAt(now);
//        mockService.saveMockResult(mockResult);
//        userService.updateVisitTime(user);
//        result.put("status", 1);
//        result.put("mockResult", mockResult);
//        return result;
//    }
//    
//    @RequestMapping(value = "/listening/result", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> saveListeningResult(@RequestHeader String token, @RequestBody MockResultDto resultDto) {//remember add user logic
//        Map<String, Object> result = new HashMap<String, Object>();
//        User user = userService.findByToken(token);
//        if(null == user) {
//            result.put("status", -2);
//            result.put("message", "invalid token");
//            return result;
//        }
//        if (TPOUtils.isAnonymous(user)) {
//            result.put("status", 0);
//            result.put("message", "anonymous can't access");
//            return result;
//            
//        }
//        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
//            result.put("status", -1);
//            result.put("message", "token disabled over 7 days, please login");
//            return result;
//        }
//        MkResult mockResult = new MkResult();
//        Date now = new Date();
//        mockResult.setUserId(user.getId());
//        mockResult.setScore(resultDto.getScore());
//        mockResult.setRightCount(resultDto.getRightCount());
//        mockResult.setTotalCount(resultDto.getTotalCount());
//        mockResult.setMkTpoId(resultDto.getTpoId());
//        mockResult.setType(TPOConstants.LISTENING);
//        mockResult.setCreatedAt(now);
//        mockResult.setUpdatedAt(now);
//        mockService.saveMockResult(mockResult);
//        userService.updateVisitTime(user);
//        result.put("status", 1);
//        result.put("mockResult", mockResult);
//        return result;
//    }
//    //获取阅读的结果
//    @RequestMapping(value = "/reading/results", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> saveReadingResults(@RequestHeader String token, @RequestBody MockResultDto resultDto) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        User user = userService.findByToken(token);
//        if(null == user) {
//            result.put("status", -2);
//            result.put("message", "invalid token");
//            return result;
//        }
//        if (TPOUtils.isAnonymous(user)) {
//            result.put("status", 0);
//            result.put("message", "anonymous can't access");
//            return result;
//            
//        }
//        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
//            result.put("status", -1);
//            result.put("message", "token disabled over 7 days, please login");
//            return result;
//        }
//        MkResult mockResult = new MkResult();
//        Pager pager = MkSingleAnswerService.findByUserIdAndTpoId(user,resultDto);
//        Date now = new Date();
//        mockResult.setUserId(user.getId());
//        mockResult.setScore(resultDto.getScore());
//        mockResult.setRightCount(resultDto.getRightCount());
//        mockResult.setTotalCount(resultDto.getTotalCount());
//        mockResult.setMkTpoId(resultDto.getTpoId());
//        mockResult.setType(TPOConstants.READING);
//        mockResult.setCreatedAt(now);
//        mockResult.setUpdatedAt(now);
//        userService.updateVisitTime(user);
//        result.put("status", 1);
//        result.put("mockResult", mockResult);
//        result.put("rows", pager.getList());
//        result.put("total", pager.getTotalCount());
//        return result;
//    }
//    //获取听力的结果
//    @RequestMapping(value = "/listening/results", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> savelisteningResults(@RequestHeader String token, @RequestBody MockResultDto resultDto) {
//    	Map<String, Object> result = new HashMap<String, Object>();
//    	User user = userService.findByToken(token);
//    	if(null == user) {
//    		result.put("status", -2);
//    		result.put("message", "invalid token");
//    		return result;
//    	}
//    	if (TPOUtils.isAnonymous(user)) {
//    		result.put("status", 0);
//    		result.put("message", "anonymous can't access");
//    		return result;
//    		
//    	}
//    	if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
//    		result.put("status", -1);
//    		result.put("message", "token disabled over 7 days, please login");
//    		return result;
//    	}
//    	MkResult mockResult = new MkResult();
//    	Pager pager = MkSingleAnswerService.findListeningByUserIdAndTpoId(user,resultDto);
//    	Date now = new Date();
//    	mockResult.setUserId(user.getId());
//    	mockResult.setScore(resultDto.getScore());
//    	mockResult.setRightCount(resultDto.getRightCount());
//    	mockResult.setTotalCount(resultDto.getTotalCount());
//    	mockResult.setMkTpoId(resultDto.getTpoId());
//    	mockResult.setType(TPOConstants.LISTENING);
//    	mockResult.setCreatedAt(now);
//    	mockResult.setUpdatedAt(now);
//    	userService.updateVisitTime(user);
//    	result.put("status", 1);
//    	result.put("mockResult", mockResult);
//    	result.put("rows", pager.getList());
//    	result.put("total", pager.getTotalCount());
//    	return result;
//    }
//    //保存学生写的结果
//    @RequestMapping(value="/save/answer",method=RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> saveSingleAnswer(@RequestHeader String token,
//    		@RequestBody MkSingleAnswerVo mkSingleAnswerVo){
//    	Map<String, Object> result = new HashMap<String,Object>();
//    	User user = userService.findByToken(token);
//	    if(null == user) {
//           result.put("status", -2);
//           result.put("message", "invalid token");
//           return result;
//        }
//        if (TPOUtils.isAnonymous(user)) {
//           result.put("status", 0);
//           result.put("message", "anonymous can't access");
//           return result;
//         
//        }
//        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
//           result.put("status", -1);
//           result.put("message", "token disabled over 7 days, please login");
//           return result;
//        }
//       String str =  MkSingleAnswerService.findByUserAndTpo(mkSingleAnswerVo,user);
//       if("nothing".equals(str)){
//    	   result.put("status", 1);
//    	   result.put("message", "tpoId 和 mkQuestionId 不匹配");
//    	   return result;
//       }
//       if("false".equals(str)){
//    	   result.put("status", 1);
//       	   result.put("message", "数据已经存在");
//    	   return result;
//       }
//        MkSingleAnswerService.saveMkResult(mkSingleAnswerVo,user);
//    
//        MkSingleAnswerService.saveMkSingleAnswer(mkSingleAnswerVo,user.getId());
//        userService.updateVisitTime(user);
//    	result.put("status", 1);
//    	result.put("message", "保存成功");
//    	return result;
//    }
//}
